package com.zhiying.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.zhiying.config.StatusEnum;
import com.zhiying.domain.CMS_Menu;
import com.zhiying.domain.CMS_Organization;
import com.zhiying.domain.CMS_User;
import com.zhiying.domain.Role;
import com.zhiying.domain.UserLoginLog;
import com.zhiying.domain.UserRole;
import com.zhiying.domain.UserVO;
import com.zhiying.jpa.MenuJPA;
import com.zhiying.jpa.ReLogin;
import com.zhiying.mybatis.UserMapper;
import com.zhiying.page.BasePageDTO;
import com.zhiying.page.PageInfo;
import com.zhiying.page.UserPageDTO;
import com.zhiying.service.LoginService;
import com.zhiying.service.MenuService;

@Transactional
@Service
@Component
public class MenuServiceImpl  implements MenuService{


	@Autowired
	UserMapper userMapper;

	@Autowired
	MenuJPA menu;

	@Autowired
	ReLogin relogin;
	@Autowired
	LoginService  loginService;
	@Override
	public List<CMS_Menu> SelectMenuOneLIst() {
		// TODO Auto-generated method stub
		return menu.SelectMenuOneLIst();
	}

	@Override
	public List<CMS_Menu> SelectMenusubmenuList() {
		// TODO Auto-generated method stub
		return menu.SelectMenusubmenuList();
	}

	@Override
	public UserPageDTO selectByPage(PageInfo pageInfo, String search) {
		Page<UserVO> page = new Page<>(pageInfo.getCurrent(), pageInfo.getLimit());
		List<UserVO> userVOs = userMapper.selectByPage(pageInfo, search, page);
		pageInfo.setTotal(selectCMSUserCount(search));
		return new UserPageDTO(userVOs, pageInfo);

	}

	@Override
	public CMS_User selectRole() {
		// TODO Auto-generated method stub
		return  menu.selectRole();
	}

	@Override
	public int deleteUserByUserId(Long id) {
		// TODO Auto-generated method stub
		return userMapper.deleteUserById(id);
	}

	@Override
	public CMS_User selectUserById(Long id) {
		// TODO Auto-generated method stub
		return menu.selectUserById(id);
	}

	@Override
	public List<Role> selectRoleList( Integer status) {
		// TODO Auto-generated method stub
		return menu.selectRoleList(status);
	}

	@Override
	public List<Role> listByUserId(Integer status, Long userId) {
		// TODO Auto-generated method stub
		return userMapper.listByUserIds(status, userId);
	}

	@Override
	public List<CMS_Organization> listBySataus(Integer status) {
		// TODO Auto-generated method stub
		return menu.listBySataus(status);
	}

	@Override
	public int updateUserById(CMS_User cms_user, String[] values,int org_id) {
		// TODO Auto-generated method stub
		if (values != null && values.length > 0) {
			List<UserRole> userRoles = new ArrayList<>();
			for (int i = 0; i < values.length; i++) {
				UserRole userRole = new UserRole();
				userRole.setUserId(cms_user.getUserId());
				userRole.setCreateTime(new Date());
				userRole.setRoleId(Long.valueOf(values[i]));
				userRole.setCreateBy(cms_user.getUserName());
				userRoles.add(userRole);
			}
			// 插入角色列表
			userMapper.insertUserRoles(userRoles);
		}
		int count=userMapper.updateUserById(org_id,cms_user);
		return count;
	}

	@Override
	public int deleteUserRole(int id) {
		// TODO Auto-generated method stub
		return userMapper.deleteUserRole(id);
	}

	@Override
	public int updateStatus(CMS_User cms_user) {
		CMS_User user = menu.selectUserById(cms_user.getUserId());
		if (user != null && StatusEnum.NORMAL.getStatus().equals(user.getStatus())) {
			CMS_User updateUser = new CMS_User();
			updateUser.setUserId(cms_user.getUserId());
			updateUser.setStatus(0);
			return userMapper.updateStatus(updateUser);
		} else if (user != null && StatusEnum.FREEZE.getStatus().equals(user.getStatus())) {
			CMS_User updateUser = new CMS_User();
			updateUser.setUserId(cms_user.getUserId());
			updateUser.setStatus(1);
			return userMapper.updateStatus(updateUser);
		}
		return 0;
	}

	@Override
	public BasePageDTO<UserLoginLog> listByPage(Long userId, PageInfo pageInfo, String search) {
		System.out.println("==========================>"+Integer.valueOf(String.valueOf(userId)));
		System.out.println("========================count===>"+selectCMSUserLoginLog(Integer.valueOf(String.valueOf(userId)),search));
		// TODO Auto-generated method stub
		Page<BasePageDTO> page=new Page<>(pageInfo.getCurrent(),pageInfo.getLimit());
		List<UserLoginLog> list=userMapper.listByPage(userId, pageInfo, search, page);
		pageInfo.setTotal(selectCMSUserLoginLog(Integer.valueOf(String.valueOf(userId)),search));
		return new BasePageDTO<UserLoginLog>(pageInfo, list);
	}

	@Override
	public int selectCMSUserCount(String search) {
		// TODO Auto-generated method stub
		return userMapper.selectCMSUserCount(search);
	}

	@Override
	public int selectCMSUserLoginLog(int userId,String search) {
		// TODO Auto-generated method stub
		return userMapper.selectCMSUserLoginLog(userId,search);
	}

	@Override
	public int insertLoginLog(UserLoginLog userLoginLog) {
		// TODO Auto-generated method stub
		CMS_User user=loginService.getUserInfoByUserName((String)SecurityUtils.getSubject().getSession().getAttribute("loginName"));
		
		return userMapper.insertLoginLog(userLoginLog);
	}

	@Override
	public int insertNewUser(CMS_User cmsUser, int organizationId,String [] role) {
		int count=userMapper.insertNewUser(cmsUser, organizationId);
		if(count>0) {
			CMS_User cms_user=relogin.getUserInfoByUserName(cmsUser.getLoginName());
			CMS_User users=loginService.getUserInfoByUserName((String)SecurityUtils.getSubject().getSession().getAttribute("loginName"));
			if (role != null && role.length > 0) {
				List<UserRole> userRoles = new ArrayList<>();
				for (int i = 0; i < role.length; i++) {
					UserRole userRole = new UserRole();
					userRole.setUserId(cms_user.getUserId());
					userRole.setCreateTime(new Date());
					userRole.setRoleId(Long.valueOf(role[i]));
					userRole.setCreateBy(users.getUserName());
					userRoles.add(userRole);
				}
				// 插入角色列表
				userMapper.insertUserRoles(userRoles);
			}
		}

		return count;
	}

	@Override
	public int updateUserPasswordById(String password, int userId) {
		// TODO Auto-generated method stub
		return userMapper.updateUserPasswordById(password, userId);
	}

	@Override
	public String selectUserByPassword(String nowpassword) {
		// TODO Auto-generated method stub
		CMS_User user=loginService.getUserInfoByUserName((String)SecurityUtils.getSubject().getSession().getAttribute("loginName"));
		ByteSource credentialsSalt = ByteSource.Util.bytes(user.getLoginName());
		System.out.println(credentialsSalt);
		Object obj = new SimpleHash("MD5", nowpassword, credentialsSalt, 6);
		return userMapper.selectUserByPassword(obj.toString());
	}

	@Override
	public int updateInfos(String password, String nowpassword) {
		int userId=Integer.valueOf(selectUserByPassword(nowpassword));
		CMS_User user=loginService.getUserInfoByUserName((String)SecurityUtils.getSubject().getSession().getAttribute("loginName"));
		ByteSource credentialsSalt = ByteSource.Util.bytes(user.getLoginName());
		System.out.println(credentialsSalt);
		Object obj = new SimpleHash("MD5", password, credentialsSalt, 6);
		return updateUserPasswordById(obj.toString(),userId);
	}

	@Override
	public int updateAdminInfoByUserId(CMS_User cmsuser) {
		// TODO Auto-generated method stub
		return userMapper.updateAdminInfoByUserId(cmsuser);
	}



}
